<template>
  <el-dialog
    v-model="dialogVisible"
    title="案件详情"
    width="70%"
    :before-close="handleClose"
  >
    <div v-loading="loading" class="case-detail">
      <div v-if="caseData" class="detail-content">
        <!-- 基本信息 -->
        <div class="info-section">
          <h3 class="section-title">基本信息</h3>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="案件ID">
              {{ caseData.id }}
            </el-descriptions-item>
            <el-descriptions-item label="案件编号">
              <span class="case-number">{{ caseData.caseNumber }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="案件状态">
              <el-tag :type="getStatusTagType(caseData.status)">
                {{ CASE_STATUS[caseData.status] }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="审理法院">
              {{ caseData.court }}
            </el-descriptions-item>
            <el-descriptions-item label="创建时间">
              {{ caseData.createTime }}
            </el-descriptions-item>
            <el-descriptions-item label="关联文档">
              <div v-if="relatedDocument" class="related-document">
                <el-tag :type="getDocTypeTagType(relatedDocument.docType)" size="small">
                  {{ DOC_TYPES[relatedDocument.docType] }}
                </el-tag>
                <span class="doc-title">{{ relatedDocument.title }}</span>
              </div>
              <span v-else class="no-document">暂无关联文档</span>
            </el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 当事人信息 -->
        <div class="info-section">
          <h3 class="section-title">当事人信息</h3>
          <div class="parties-container">
            <div class="parties-content">
              {{ caseData.parties }}
            </div>
          </div>
        </div>

        <!-- 关联文档详情 -->
        <div v-if="relatedDocument" class="info-section">
          <h3 class="section-title">关联文档信息</h3>
          <div class="document-info">
            <el-descriptions :column="1" border>
              <el-descriptions-item label="文档标题">
                {{ relatedDocument.title }}
              </el-descriptions-item>
              <el-descriptions-item label="文档类型">
                <el-tag :type="getDocTypeTagType(relatedDocument.docType)">
                  {{ DOC_TYPES[relatedDocument.docType] }}
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="发布日期">
                {{ relatedDocument.publishDate }}
              </el-descriptions-item>
              <el-descriptions-item label="文档来源">
                {{ relatedDocument.source }}
              </el-descriptions-item>
              <el-descriptions-item label="关键词">
                <div class="keywords">
                  <el-tag
                    v-for="keyword in getKeywords(relatedDocument.keywords)"
                    :key="keyword"
                    size="small"
                    style="margin-right: 5px; margin-bottom: 5px;"
                  >
                    {{ keyword }}
                  </el-tag>
                </div>
              </el-descriptions-item>
            </el-descriptions>
          </div>
        </div>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="handleEdit">
          <el-icon><Edit /></el-icon>
          编辑案件
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Edit } from '@element-plus/icons-vue'
import { getCaseDetail, CASE_STATUS_OPTIONS, CASE_STATUS } from '../../../api/case'
import { getDocumentDetail, DOC_TYPES } from '../../../api/document'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  caseId: {
    type: [Number, String],
    default: null
  }
})

const emit = defineEmits(['update:visible', 'edit'])

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

// 加载状态
const loading = ref(false)

// 案件数据
const caseData = ref(null)

// 关联文档数据
const relatedDocument = ref(null)

// 获取状态标签类型
const getStatusTagType = (status) => {
  const option = CASE_STATUS_OPTIONS.find(opt => opt.value === status)
  return option?.type || 'info'
}

// 获取文档类型标签样式
const getDocTypeTagType = (docType) => {
  const typeMap = {
    0: 'danger',   // 法律 - 红色
    1: 'primary',  // 案例 - 蓝色
    2: 'warning',  // 判决书 - 橙色
    3: 'success'   // 文章 - 绿色
  }
  return typeMap[docType] || 'info'
}

// 处理关键词
const getKeywords = (keywords) => {
  if (!keywords) return []
  return keywords.split(',').filter(k => k.trim())
}

// 获取案件详情
const fetchCaseDetail = async () => {
  if (!props.caseId) return
  
  loading.value = true
  try {
    const response = await getCaseDetail(props.caseId)
    if (response.code === 200) {
      caseData.value = response.data
      
      // 如果有关联文档，获取文档详情
      if (response.data.documentId) {
        await fetchRelatedDocument(response.data.documentId)
      }
    } else {
      ElMessage.error('获取案件详情失败')
    }
  } catch (error) {
    console.error('获取案件详情失败:', error)
    ElMessage.error('获取案件详情失败')
  } finally {
    loading.value = false
  }
}

// 获取关联文档详情
const fetchRelatedDocument = async (documentId) => {
  try {
    const response = await getDocumentDetail(documentId)
    if (response.code === 200) {
      relatedDocument.value = response.data
    }
  } catch (error) {
    console.error('获取关联文档详情失败:', error)
  }
}

// 关闭对话框
const handleClose = () => {
  caseData.value = null
  relatedDocument.value = null
  emit('update:visible', false)
}

// 编辑案件
const handleEdit = () => {
  emit('edit', caseData.value)
  handleClose()
}

// 监听caseId变化，获取详情
watch(() => props.caseId, (newId) => {
  if (newId && props.visible) {
    fetchCaseDetail()
  }
}, { immediate: true })

// 监听对话框打开
watch(() => props.visible, (visible) => {
  if (visible && props.caseId) {
    fetchCaseDetail()
  }
})
</script>

<style scoped>
.case-detail {
  min-height: 300px;
}

.detail-content {
  padding: 0 10px;
}

.info-section {
  margin-bottom: 30px;
}

.section-title {
  margin: 0 0 15px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  border-left: 4px solid #409EFF;
  padding-left: 10px;
}

.case-number {
  font-weight: 600;
  font-size: 16px;
  color: #333;
}

.parties-container {
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  background-color: #fafafa;
  padding: 20px;
}

.parties-content {
  line-height: 1.8;
  font-size: 14px;
  color: #333;
  white-space: pre-wrap;
  word-break: break-word;
}

.related-document {
  display: flex;
  align-items: center;
  gap: 10px;
}

.doc-title {
  font-weight: 500;
  color: #333;
}

.no-document {
  color: #999;
  font-style: italic;
}

.document-info {
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
}

.keywords {
  line-height: 1.8;
}

.dialog-footer {
  text-align: right;
}

:deep(.el-descriptions__label) {
  width: 120px;
  font-weight: 600;
}

:deep(.el-descriptions__content) {
  word-break: break-word;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .detail-content {
    padding: 0 5px;
  }
  
  .parties-container {
    padding: 15px;
  }
  
  .related-document {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
  
  :deep(.el-descriptions) {
    --el-descriptions-item-bordered-label-background: #fafafa;
  }
}
</style>
